package com.calefk.util
{
	import flash.display.*;
	import flash.filters.ColorMatrixFilter;
	import flash.filters.GlowFilter;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.utils.ByteArray;
	
	/**
	 * ...
	 * @author ...
	 */
	public class helper
	{
		public static function LineIntersectsRect(  v1:Point, v2: Point,  r:Rectangle ):Boolean
		{
				var lowerLeft:Point=new Point( r.x, r.y+r.height );
				var upperRight:Point=new Point( r.x+r.width, r.y );
				var upperLeft:Point=new Point( r.x, r.y );
				var lowerRight:Point=new Point( r.x+r.width, r.y+r.height);
				// check if it is inside
				if (v1.x > lowerLeft.x && v1.x < upperRight.x && v1.y < lowerLeft.y && v1.y > upperRight.y &&
					v2.x > lowerLeft.x && v2.x < upperRight.x && v2.y < lowerLeft.y && v2.y > upperRight.y )
				{   
					return true;
				}
				// check each line for intersection
				if (LineIntersectLine(v1,v2, upperLeft, lowerLeft ) ) return true;
				if (LineIntersectLine(v1,v2, lowerLeft, lowerRight) ) return true;
				if (LineIntersectLine(v1,v2, upperLeft, upperRight) ) return true;
				if (LineIntersectLine(v1,v2, upperRight, lowerRight) ) return true;
				return false;
		}
		public static function LineIntersectLine(  v1:Point,  v2:Point,  v3:Point,  v4:Point ):Boolean
		{
			var  denom:Number = ((v4.y - v3.y) * (v2.x - v1.x)) - ((v4.x - v3.x) * (v2.y - v1.y));
			var numerator: Number = ((v4.x - v3.x) * (v1.y - v3.y)) - ((v4.y - v3.y) * (v1.x - v3.x));

			var numerator2:Number  = ((v2.x - v1.x) * (v1.y - v3.y)) - ((v2.y - v1.y) * (v1.x - v3.x));

			if ( denom == 0.0 )
			{
				if ( numerator == 0.0 && numerator2 == 0.0)
				{
					return false;//COINCIDENT;
				}
				return false;// PARALLEL;
			}
			var  ua:Number = numerator / denom;
			var ub:Number = numerator2/ denom;

			return (ua >= 0.0 && ua <= 1.0 && ub >= 0.0 && ub <= 1.0);
		}
		public static function drawRectContainer(contain:DisplayObjectContainer,alpha:Number=1,color:uint=0x000000):void
		{
			var square:Sprite = new Sprite();
			contain.addChild(square);
			square.graphics.lineStyle(0, color);
			square.graphics.beginFill(color);
			square.graphics.drawRect(0, 0, contain.width, contain.height);
			square.graphics.endFill();
			square.x = 0;
			square.y = 0;
			square.alpha = 0.75;
		}
		public static function clone(source:Object):*
		{
			var myBA:ByteArray = new ByteArray();
			myBA.writeObject(source);
			myBA.position = 0;
			return(myBA.readObject());
		}
		public static function scaleBitmap( ARG_object:DisplayObject, ARG_scaleX:Number, ARG_scaleY:Number ):Bitmap {
			// create a BitmapData object the size of the crop
			var bmpd:BitmapData = new BitmapData(ARG_object.width * ARG_scaleX, ARG_object.height * ARG_scaleY, true, 0xffffff);
			// create the scaled Bitmap object from the BitmapData
			var scaledBitmap:Bitmap = new Bitmap(bmpd, PixelSnapping.ALWAYS, true);
			// create the matrix that will perform the scaling
			var scaleMatrix:Matrix = new Matrix();
			scaleMatrix.scale(ARG_scaleX, ARG_scaleY);
			// draw the object to the BitmapData, applying the matrix to scale
			bmpd.draw( ARG_object, scaleMatrix );
			return scaledBitmap; // return the scaled Bitmap
		}
		public static function BitmapdataScaled(do_source:BitmapData, thumbWidth:Number, thumbHeight:Number):BitmapData {
			var mat:Matrix = new Matrix();
			mat.scale(thumbWidth/do_source.width, thumbHeight/do_source.height);
			var bmpd_draw:BitmapData = new BitmapData(thumbWidth, thumbHeight, true,0xffffff);
			bmpd_draw.draw(do_source, mat, null, null, null,true);
			return bmpd_draw;
		}
		public static function removeItemArray(thearray:Array, theItem:Object):void
		{
			//trace("Remove "+theItem+" from "+thearray);
			for (var i:int = 0; i < thearray.length; i++)
			{
				if (thearray[i] == theItem)
				{
					thearray.splice(i, 1);
					i -= 1;
				}
			}
			//trace("Your array is now - "+thearray);
		}
		
		public static function randomInterval(Min:int, Max:int):int
		{
			if (Min < Max)
			{
				return (Min + (int)(Math.random() * ((Max - Min) + 1)));
			}
			else
			{
				return (Max + (int)(Math.random() * ((Min - Max) + 1)))
			}
		}
		
		public static function degreesToRadian(angle:Number):Number
		{
			return angle * (Math.PI / 180)
		}
		
		public static function snapShot(mc:DisplayObject, scale:Number = 1):Bitmap
		{
			var m:Matrix = new Matrix();
			m.scale(scale, scale);
			var bmpData:BitmapData = new BitmapData(mc.width, mc.height, true, 0x00000000);
			bmpData.draw(mc, m);
			var bmp:Bitmap = new Bitmap(bmpData);
			return bmp;
		}
		
		public static function drawRectangle(width:Number = 800, height:Number = 600, color:uint = 0xFFFFFF, borderColor:uint = 0xFFFFFF, alpha:Number = 1):Sprite
		{
			var square:Sprite = new Sprite();
			//square.graphics.lineStyle(0, borderColor);
			square.graphics.beginFill(color);
			square.graphics.drawRect(0, 0, width, height);
			square.graphics.endFill();
			return square
		
		}
		public static function transformColorMovieClip(movie:MovieClip):void 
		{
			var matrix:Array = new Array();
			matrix = matrix.concat([0.09, 0, 0, 0, -25]); // red
			matrix = matrix.concat([0.13, 0.65, 0, 0, 0]); // green
			matrix = matrix.concat([0.44, 1, 0, 0, 0]); // blue
			matrix = matrix.concat([0, 0, 0, 0.75, 0]); // alpha
			var my_filter:ColorMatrixFilter=new ColorMatrixFilter(matrix);
			movie.filters= [my_filter];
		}
		public static function getText(text:String):TextField {
			var campo:TextField = new TextField();
			
			campo.selectable = false;
			campo.text = text;
			return campo;
			
			
		}
		
		public static function dibujarLinea(p1:Point,p2:Point,color:uint = 0xFFFFFF):Sprite {
			var line:Sprite = new Sprite();
			line.graphics.lineStyle(5, 16711680, 100, true, "none", "round", "round", 1);
			line.graphics.moveTo(p1.x, p1.y);
			line.graphics.lineTo(p2.x, p2.y);
			line.filters= [new GlowFilter(0x00CCFF, 1, 10, 10, 2, 1, false, false)];
			return line;
		}
		public static function verifyInterval(max:Number,number:Number,minimum:Number):Boolean
		{
			return (max >= number && number > minimum);
		}
	}

}